﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace TestSocket
{
    class CTextMessage:CBaseRequest
    {
        string m_strMessage = string.Empty;
        readonly string strElementName = "TextMessage";        
        public string strMessage { get { return m_strMessage; } set { m_strMessage = value; } }
        
        public override void Reset()
        {
            m_strMessage = string.Empty;
            m_strXMLFragment = string.Empty;
        }

        public override void OnElement(string strName, Dictionary<string, string> Attribs){}

        public override void OnElementValue(string strName, string strValue)
        {
            if (strName == strElementName)
                m_strMessage = strValue;
        }

        protected override void toXML(System.Xml.XmlWriter writer)
        {
            writer.WriteStartElement(strElementName);
            writer.WriteString(m_strMessage);
            writer.WriteEndElement();
        }
        
    }

    class CFileSendRequest: CBaseRequest
    {        
        readonly string strReqElementName = "FileSendReq";                
        public string m_strFileName = string.Empty;
        public long m_lFileSize = 0;
        public string strSocketAddr=string.Empty;
        public string strFilePath 
        { 
            set 
            {
                FileInfo info = new FileInfo(value);
                m_strFileName = info.Name;
                m_lFileSize = info.Length;                
            } 
        }
        
        public override void Reset() 
        {
            m_strFileName = string.Empty;            
            m_lFileSize = 0;
            strSocketAddr = string.Empty;            
            m_strXMLFragment = string.Empty;
        }

        public override void OnElement(string strName, Dictionary<string, string> Attribs){}          

        public override void OnElementValue(string strName, string strValue)
        {
            if (strName == "FileName")
                m_strFileName = strValue;
            else if (strName == "SocketAddr")
                strSocketAddr = strValue;
            else if (strName == "FileSize")
                m_lFileSize = long.Parse(strValue);
        }

        public override void OnElementClosed(string strName) { }        
        

        protected override void toXML(System.Xml.XmlWriter writer)
        {                        
            writer.WriteStartElement(strReqElementName);                
            writer.WriteElementString("FileName", m_strFileName);
            writer.WriteElementString("FileSize", m_lFileSize.ToString());
            writer.WriteElementString("SocketAddr", strSocketAddr);
            writer.WriteEndElement();
            
        }
    }

    class CFileSendResp:CBaseRequest
    {
        public bool bReady2Recv = false;

        public override void Reset()
        {
            bReady2Recv = false;
            strXMLFragment = string.Empty;
        }

        public override void OnElementValue(string strName, string strValue)
        {
            if (strName == "Status")
                bReady2Recv = (strValue == "Ready");
        }
        protected override void toXML(System.Xml.XmlWriter writer)
        {
            writer.WriteStartElement("FileSendResp");
            writer.WriteElementString("Status", bReady2Recv ? "Ready" : "FAILED");            
            writer.WriteEndElement();
        }
    }

    //used to notify server binary content finished sending over
    //server will close binaryStreamwriter     
    
}



